package com.epsoft.odin7cj.flow.bpo._impl.activity;

import java.util.List;
import java.util.Map;

import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.repository.DiagramElement;
import org.camunda.bpm.engine.repository.DiagramLayout;
import org.camunda.bpm.engine.runtime.ActivityInstance;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.Query;
import org.camunda.bpm.model.bpmn.impl.instance.UserTaskImpl;
import org.camunda.bpm.model.bpmn.instance.FlowNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.epsoft.odin7cj.flow.bpo.activity.ActivityBPO;

/**  
 * 活动节点BOP实现类 
 * @author 胡义振  
 * @date 2020年06月26日  
*/
@Service 
public class ActivityBPOImpl implements ActivityBPO {

	@Autowired
	private HistoryService historyService;
	
	@Autowired
	private RuntimeService runtimeService;
	
	@Autowired
	private RepositoryService repositoryService;
	/**
	 * DESC: 查询最后一个节点
	 * @auther 胡义振
	 * @date 2020年6月11日
	 * @param processInstanceId
	 * @return 
	 * @return
	 */
	@Override
	public HistoricActivityInstance getLastActivityInstance(String processInstanceId) {
		List<HistoricActivityInstance> resultList = historyService.createHistoricActivityInstanceQuery()
		         .processInstanceId(processInstanceId)
		         .activityType("userTask")
		         .finished()
		         .orderByHistoricActivityInstanceEndTime()
		         .asc()
		         .list();
		// 得到最后一个任务节点的id
		HistoricActivityInstance historicActivityInstance = null;
		if(resultList!=null && resultList.size()>0) {
			historicActivityInstance = resultList.get(resultList.size()-1);
		}
		return historicActivityInstance;
	}
	

	/**
	 * DESC: 查询第一个节点
	 * @auther 胡义振
	 * @date 2020年6月11日
	 * @param processInstanceId
	 * @return 
	 * @return
	 */
	@Override
	public HistoricActivityInstance getFirstActivityInstance(String processInstanceId) {
		List<HistoricActivityInstance> resultList = historyService.createHistoricActivityInstanceQuery()
		         .processInstanceId(processInstanceId)
		         .activityType("userTask")
		         .finished()
		         .orderByHistoricActivityInstanceEndTime()
		         .asc()
		         .list();
		// 得到最后一个任务节点的id
		HistoricActivityInstance historicActivityInstance = null;
		if(resultList!=null && resultList.size()>0) {
			historicActivityInstance = resultList.get(0);
		}
		return historicActivityInstance;
	}
	

	/**
	 * DESC: 获取实例ID
	 * @auther 胡义振
	 * @date 2020年6月11日
	 * @param processInstanceId
	 * @param activityId
	 * @return 
	 * @return
	 */
	@Override
	public String getInstanceIdForActivity(String processInstanceId, String activityId) {
		ActivityInstance activityInstance = runtimeService.getActivityInstance(processInstanceId);
		ActivityInstance instance = getChildInstanceForActivity(activityInstance, activityId);
		if (instance != null) {
			return instance.getId();
		}
		return null;
	}

	private ActivityInstance getChildInstanceForActivity(ActivityInstance activityInstance, String activityId) {
		if (activityId.equals(activityInstance.getActivityId())) {
			return activityInstance;
		}
		for (ActivityInstance childInstance : activityInstance.getChildActivityInstances()) {
			ActivityInstance instance = getChildInstanceForActivity(childInstance, activityId);
			if (instance != null) {
				return instance;
			}
		}
		return null;
	}


	/**
	 * DESC: 获取前一个节点ID
	 * @auther 胡义振
	 * @date 2020年6月27日
	 * @param currActivityId
	 * @param processDefinitionId
	 * @return 
	 * @return
	 */
	@Override
	public String getPreActivityId(String currActivityId, String processDefinitionId) {
		
		/** 查询所有节点信息 **/
		DiagramLayout diagramLayout = repositoryService.getProcessDiagramLayout(processDefinitionId);
		DiagramElement currDiagramElement = null;
		Map<String, DiagramElement>  elements = diagramLayout.getElements();
		for(Map.Entry<String, DiagramElement> entry : elements.entrySet()) {
			if(currActivityId.equals(entry.getKey())) {
				currDiagramElement = entry.getValue();
			}
		}
		System.out.println("\n currDiagramElement:"+currDiagramElement);
		
		/** 查询模型实例 **/
		BpmnModelInstance bpmnModelInstance = repositoryService.getBpmnModelInstance(processDefinitionId);
		// 查询当前节点
		UserTaskImpl currNode = bpmnModelInstance.getModelElementById(currActivityId);
		System.out.println("\n userTask.name:"+currNode.getName());
		
		// 查询上一节点列表
		Query<FlowNode> queryPreNode = currNode.getPreviousNodes();
		UserTaskImpl preNode = (UserTaskImpl) queryPreNode.list().get(0);
		
		return preNode.getId();
		
	}

}
